package com.wx.mall.operate.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.wx.mall.operate.dto.SeckillItemDto;
import com.wx.mall.operate.entity.SeckillAdds;
import com.wx.mall.operate.entity.SeckillItem;
import com.wx.mall.operate.mapper.SeckillItemMapper;
import com.wx.mall.operate.service.ISeckillAddsService;
import com.wx.mall.operate.service.ISeckillItemService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author j
 * @since 2020-05-28
 */
@Service
public class SeckillItemServiceImpl extends ServiceImpl<SeckillItemMapper, SeckillItem> implements ISeckillItemService {

    @Autowired
    ISeckillAddsService seckillAddsService;

    @Override
    public List<SeckillItem> findItem() {
        QueryWrapper<SeckillItem> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("deletes",1).orderByAsc("money");
        List<SeckillItem> list=this.list(queryWrapper);
        return list;
    }

    @Override
    public int deleteOne(Integer id) {
        SeckillItem byId = this.getById(id);
        SeckillAdds seckillAdds=new SeckillAdds();
        seckillAdds.setId(byId.getNumber());
        SeckillAdds byId1 = seckillAddsService.getById(seckillAdds.getId());
        byId1.setDifferenceType(1);
        boolean b = seckillAddsService.updateById(byId1);
        boolean b1 = this.removeById(byId.getId());
        return b||b1?1:0;
    }

    @Override
    public int updateOne(SeckillItemDto dto) {
        SeckillItem byId = this.getById(dto.getId());
        byId.setSeckillMoney(dto.getSeckillMoney());
        byId.setSeckillNum(dto.getSeckillNum());
        byId.setPurchaseLimit(dto.getPurchaseLimit());
        byId.setSort(dto.getSort());
        boolean b = this.updateById(byId);
        return b?1:0;
    }
}
